package com.cskaoyan.service;

import com.cskaoyan.bean.baseresultVO.BaseData;
import com.cskaoyan.bean.baseresultVO.BaseParam;
import com.cskaoyan.bean.collect.CollectBO;
import com.cskaoyan.bean.collect.CollectVO;
import com.cskaoyan.bean.domain.Collect;
import com.cskaoyan.bean.domain.CollectExample;
import com.cskaoyan.bean.domain.Goods;
import com.cskaoyan.mapper.CollectMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.utils.MyUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    CollectMapper mapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public void addordelete(CollectBO collectBO, Integer userId) {

        Byte type = collectBO.getType();
        Integer valueId = collectBO.getValueId();
        Collect collectResult = mapper.addordeleteSelect1(type, valueId, userId);

        if (collectResult == null) {
            Date date = new Date();
            Collect collect = new Collect();

            collect.setUserId(userId);
            collect.setValueId(valueId);
            collect.setType(type);
            collect.setAddTime(date);
            collect.setUpdateTime(date);
            collect.setDeleted(false);

            mapper.insertSelective(collect);
        } else {
            Boolean deleted = collectResult.getDeleted();
            if (deleted.equals(true)) {
                collectResult.setDeleted(false);
                collectResult.setUpdateTime(new Date());
                mapper.updateByPrimaryKeySelective(collectResult);
            } else {
                collectResult.setDeleted(true);
                collectResult.setUpdateTime(new Date());
                mapper.updateByPrimaryKeySelective(collectResult);
            }
        }

    }

    @Override
    public BaseData getList(Integer page, Integer limit, Byte type, Integer userId){
        PageHelper.startPage(page, limit);
        CollectExample example = new CollectExample();
        CollectExample.Criteria criteria = example.createCriteria();

        criteria.andUserIdEqualTo(userId);
        criteria.andTypeEqualTo(type);
        criteria.andDeletedEqualTo(false);
//        if (param.getSort() != null && param.getOrder() != null) {
//            example.setOrderByClause(param.getSort() + " " + param.getOrder());
//        }
        List<Collect> collectList = mapper.selectByExample(example);



        List<CollectVO> collectVOList = new ArrayList<>();
        Integer valueId;
        Byte type1;
        CollectVO collectVO = new CollectVO();
        for (Collect collect : collectList) {
            type1 = collect.getType();
            if (type1.equals((byte) 0)) {
                Integer id = collect.getId();
                valueId = collect.getValueId();

                Goods goods = goodsMapper.selectByPrimaryKey(valueId);
                String brief = goods.getBrief();
                String name = goods.getName();
                String picUrl = goods.getPicUrl();
                BigDecimal retailPrice = goods.getRetailPrice();

                collectVO.setBrief(brief);
                collectVO.setPicUrl(picUrl);
                collectVO.setValueId(valueId);
                collectVO.setName(name);
                collectVO.setId(id);
                collectVO.setType(type1);
                collectVO.setRetailPrice(retailPrice);

                collectVOList.add(collectVO);

            }
        }


        PageInfo<CollectVO> pageInfo = new PageInfo<>(collectVOList);
        Integer total = MyUtils.getInteger(pageInfo.getTotal());
        return new BaseData(pageInfo.getPageNum(), pageInfo.getSize(), total, pageInfo.getPages(), collectVOList);
    }
}
